package com.landiao.ctrler.management;

import java.util.List;
import java.util.ArrayList;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSON;
import com.landiao.common.factory.ResultMsgFactory;
import com.landiao.common.pojo.ZTree;
import com.landiao.common.utils.CodecUtils;
import com.landiao.ctrler.BaseCRUDCtrler;
import com.landiao.entities.Shop;
import com.landiao.entities.ShopGrade;
import com.landiao.entities.Vip;
import com.landiao.service.ShopService;

@Controller
@RequestMapping("/management/shop")
public class ShopCtrler extends BaseCRUDCtrler<Shop, String>  {
	private ShopService getShopService() {
        return (ShopService) baseService;
    }
	@Override
	public String save(Shop t, RedirectAttributes redirectAttr) {
		// TODO Auto-generated method stub
		t.setCompId(this.getClientUser().getUser().getCompId());
		t.setShopId(CodecUtils.getUUID());
		t.setPhoto(this.getphoto(t.getPhoto()));
		baseService.save(t);
		redirectAttr.addFlashAttribute("resultMsg",
				ResultMsgFactory.success("添加成功"));
		return redirectToUrl(viewName("index"));
	}
	
	@Override
	public String update(Shop t, RedirectAttributes redirectAttr) {
		// TODO Auto-generated method stub
		t.setPhoto(this.getphoto(t.getPhoto()));
		return super.update(t, redirectAttr);
	}
	@RequestMapping(value = "add/{id}", method = RequestMethod.GET)
	public String add(Model model,@PathVariable String id) {
		Shop t = getShopService().getById(id);
		model.addAttribute("t",t);
		return viewName("add");
	}

	@ResponseBody
	@RequestMapping("listshop")
	public List<Shop> listshop(Shop shop){
		String shopId = this.getClientUser().getUser().getShopId();
		List<Shop> shops = this.getShopService().getSelfAndChildList(shopId);
		return shops;
	}
	@ResponseBody
	@RequestMapping("/shopInfo/{shopId}")
	public Shop shopInfo(@PathVariable String shopId){
		Shop shop = getShopService().getById(shopId);
		return shop;
	}
	@ResponseBody
	@RequestMapping("listshopGrade")
	public List<ShopGrade> listshopGrade(ShopGrade shopGrade){
		List<ShopGrade> list = getShopService().getListshopGrade(shopGrade);
		return list;
	}
	@RequestMapping("shopTree")
	public String shopTree(Model model) {
		String shopId = this.getClientUser().getUser().getShopId();
		List<Shop> shops = this.getShopService().getSelfAndChildList(shopId);
		List<ZTree> shopTrees = new ArrayList<>();
		if(shops!=null&&shops.size()>0){
			for(Shop shop:shops){
				ZTree ztree = new ZTree();
				ztree.setId(shop.getShopId());
				ztree.setName(shop.getName());
				ztree.setpId(shop.getPshopId());
				ztree.setOpen(true);
				
				shopTrees.add(ztree);
			}
		}
		model.addAttribute("shopTrees", shopTrees);
		return viewName("shopTree");
	}
}
